package com.nova.edc.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.nova.edc.dao.system.CustomerInfoDao;
import com.nova.edc.dao.system.CustomerRegionDao;
import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.model.system.CustomerRegion;
import com.nova.edc.model.system.SystemData;
import com.nova.edc.service.system.CustomerRegionService;
import com.nova.frame.dao.buzz.query.Order;
import com.nova.frame.dao.buzz.query.Query;
import com.nova.frame.dao.criterion.Restrictions;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.JdbcUtils;
import com.nova.module.filter.query.Filter;

public class CustomerRegionServiceImpl implements CustomerRegionService {

	@Autowired
	private CustomerRegionDao novaCustomerRegionDao;

	@Autowired
	private CustomerInfoDao novaCustomerInfoDao;

	@Override
	public PageList<CustomerRegion> findPageList(CustomerRegion customerRegion,
			Page page, String sidx, String sord) {
		Query<CustomerRegion> query = novaCustomerRegionDao.query()
				.add(Restrictions.eq("t_mapcustomerinfo.Scrapped", 0))
				.addOrder(Order.custom(sord, sidx));
		Filter.addFilterCondition(query, customerRegion);
		return query.pageList(page);
	}

	@Override
	public CustomerRegion getById(int id) {
		return novaCustomerRegionDao.getById(String.valueOf(id));
	}

	@Override
	public CustomerRegion insert(CustomerRegion customerRegion) {
		CustomerInfo customerInfo = novaCustomerInfoDao.getById(customerRegion.getCustomerinfoid());
		customerRegion.setCompanycode(customerInfo.getCompanycode());
		return novaCustomerRegionDao.insert(customerRegion);
	}

	@Override
	public int update(CustomerRegion customerRegion) {
		return novaCustomerRegionDao.updateNotNull(customerRegion);
	}

	@Override
	public int delete(CustomerRegion customerRegion) {
		customerRegion.setScrapped(1);
		return novaCustomerRegionDao.updateNotNull(customerRegion);
	}

	@Override
	public int deleteById(int id) {
		CustomerRegion customerRegion = getById(id);
		customerRegion.setScrapped(1);
		return update(customerRegion);
	}

	@Override
	public CustomerRegion getByCompanyId(int customerinfoid) {
		return novaCustomerRegionDao.query()
				.add(Restrictions.eq("t_mapcustomerinfo.CustomerInfoId", customerinfoid))
				.add(Restrictions.eq("t_mapcustomerinfo.Scrapped", 0)).get();
	}

	@Override
	public List<CustomerRegion> findAll() {
		return novaCustomerRegionDao.query().list();
	}

	@Override
	public List<CustomerRegion> findByDistinctRegion(int loginId) {
		String sqlLimit = "select t_mapcustomerinfo.CompanyRegion,count(t_mapcustomerinfo.CompanyRegion) companycount"
				+ " from t_mapcustomerinfo"
				+ " left join t_login"
				+ " on t_login.CustomerInfoID=t_mapcustomerinfo.customerinfoid"
				+ " left join t_role"
				+ " on t_login.Roleid=t_role.ID"
				+ " where t_login.ParentId=? and t_login.Scrapped=0 and t_role.RoleType = ? "
				+ " group by t_mapcustomerinfo.CompanyRegion";
		return JdbcUtils.query(CustomerRegion.class, sqlLimit, loginId,
				SystemData.ROLE_USER);
	}

	@Override
	public List<CustomerRegion> findByLoginRegion(int loginId,
			String companyRegion) {
		String sqlLimit = "select t_mapcustomerinfo.*,t_customerinfo.*"
				+ " from t_mapcustomerinfo"
				+ " left join t_login"
				+ " on t_login.CustomerInfoID=t_mapcustomerinfo.CustomerInfoId"
				+ " left join t_customerinfo"
				+ " on t_customerinfo.ID=t_mapcustomerinfo.CustomerInfoId"
				+ " left join t_role"
				+ " on t_login.Roleid=t_role.ID"
				+ " where t_login.ParentId=? and t_mapcustomerinfo.Scrapped=0 "
				+ "and t_role.RoleType = ? and t_mapcustomerinfo.CompanyRegion=?";
		return JdbcUtils.query(CustomerRegion.class, sqlLimit, loginId,
				SystemData.ROLE_USER, companyRegion);
	}
}
